package com.huluobo.a2107monthdemo.category

import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.huluobo.a2107monthdemo.R
import com.huluobo.a2107monthdemo.base.BaseMVVMFragment
import com.huluobo.a2107monthdemo.databinding.FragmentCategoryBinding

class CategoryFragment : BaseMVVMFragment<FragmentCategoryBinding>() {
    private lateinit var viewModel: CategoryViewModel

    //一级列表的adapter
    private lateinit var parentAdapter: CategoryParentAdapter

    //一级列表的数据源
    private val categoryParentList = mutableListOf<CategoryBean>()

    //二级列表的适配器
    private lateinit var childAdapter: CategoryChildAdapter

    //二级列表的数据源
    private val categoryChildList = mutableListOf<CategoryBean>()

    //三级列表的适配器
    private lateinit var goodsAdapter: CategoryGoodsListAdapter

    //三级列表的数据源
    private val goodsList = mutableListOf<CategoryGoodsListBean>()
    override fun getLayoutId(): Int = R.layout.fragment_category

    override fun initData() {
        viewModel = ViewModelProvider(this)[CategoryViewModel::class.java]
        viewModel.getCategory()

        viewModel.success.observe(requireActivity()) {
            if (it.code == 200) {
                categoryParentList.clear()
                categoryParentList.addAll(it.data)
                parentAdapter.notifyDataSetChanged()

                //进入页面初始化时,需要同时加载二级列表中的数据
                val data = categoryParentList[0].data
                if (data != null) {
                    categoryChildList.clear()
                    categoryChildList.addAll(data)
                    childAdapter.notifyDataSetChanged()

                    val goodsId = categoryChildList[0].id
                    viewModel.getCategoryGoodsList(goodsId, 1, 10)
                }
            }
        }

        viewModel.getGoodsListSuccess.observe(requireActivity()) {
            if (it.code == 200) {
                goodsList.clear()
                goodsList.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        //初始化一级列表
        dataBinding.categoryRvParent.layoutManager = LinearLayoutManager(requireContext())
        parentAdapter = CategoryParentAdapter(categoryParentList)
        dataBinding.categoryRvParent.adapter = parentAdapter

        //一级列表的点击事件
        parentAdapter.setOnItemClickListener { adapter, view, position ->
            parentAdapter.setSelectItemPosition(categoryParentList[position].id)
            if (categoryParentList[position].data != null) {
                childAdapter.setSelectItemPosition(categoryParentList[position].data!![0].id)
                dataBinding.categoryRvChild.scrollToPosition(0)
            }
            val data = categoryParentList[position].data
            if (data != null) {
                categoryChildList.clear()
                categoryChildList.addAll(data)
                childAdapter.notifyDataSetChanged()

                val goodsId = categoryChildList[0].id
                viewModel.getCategoryGoodsList(goodsId, 1, 10)
            }
        }

        //初始化二级列表
        dataBinding.categoryRvChild.layoutManager =
            GridLayoutManager(requireContext(), 2, RecyclerView.HORIZONTAL, false)
        childAdapter = CategoryChildAdapter(categoryChildList)
        dataBinding.categoryRvChild.adapter = childAdapter

        childAdapter.setOnItemClickListener { adapter, view, position ->
            if (categoryChildList.size != 0) {
                viewModel.getCategoryGoodsList(categoryChildList[position].id, 1, 10)
            }
            childAdapter.setSelectItemPosition(categoryChildList[position].id)
        }

        //初始化三级列表
        dataBinding.categoryRvGoodsList.layoutManager = LinearLayoutManager(requireContext())
        goodsAdapter = CategoryGoodsListAdapter(goodsList)
        dataBinding.categoryRvGoodsList.adapter = goodsAdapter
    }
}